package com.example.demo.design.observe_pattern;

import java.util.ArrayList;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 15:20 2021/3/26
 * @Modified By:
 */
public abstract class Subject {
    // 定义一个观察者集合用于存储所有观察者对象
    protected ArrayList<Observer> observers = new ArrayList<Observer>();

    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void detach(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObServers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}
